package com.lovesoft.prologer.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.lovesoft.prologer.ApplicationInput;
import com.lovesoft.prologer.Setup;
import com.lovesoft.prologer.Translation;
import com.lovesoft.prologer.core.LogID;
import com.lovesoft.prologer.core.LogView;

public class LogPanel extends JPanel implements LogView, ActionListener, KeyListener {
	private static final long serialVersionUID = -8162679910096596418L;
	private JTextField filterTextField;
	private JButton filterBtn;
	private JButton disablefilterBtn;
	private TextArea ta;
	private ApplicationInput applicationInput;
	private boolean filterEnabled = false;
	private int lineHeight;
	private LogID logID;

	public LogPanel(ApplicationInput applicationInput, LogID logID) {
		this.applicationInput = applicationInput;
		this.logID = logID;
		ta = new TextArea();
		ta.setEditable(false);
		filterTextField = new JTextField(20);
		filterTextField.addKeyListener(this);
		Font font = new Font("Monospaced", Font.PLAIN, 13);
		ta.setFont(font);
		ta.setForeground(Color.BLACK);
		ta.setBackground(Color.WHITE);
		filterBtn = new JButton(Translation.translate("Filter"));
		disablefilterBtn = new JButton(Translation.translate("Disable Filter"));
		JPanel toolsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		toolsPanel.add(filterTextField);
		toolsPanel.add(filterBtn);
		toolsPanel.add(disablefilterBtn);
		filterBtn.addActionListener(this);
		disablefilterBtn.addActionListener(this);
		checkWidgetsState();
		setLayout(new BorderLayout());
		add(ta, BorderLayout.CENTER);
		add(toolsPanel, BorderLayout.NORTH);
		
		FontMetrics m = ta.getFontMetrics(ta.getFont());
		lineHeight = m.getAscent() + m.getDescent();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == disablefilterBtn) {
			applicationInput.textFilterDisabled(this);
			filterEnabled = false;
			checkWidgetsState();
		}
		if (e.getSource() == filterBtn) {
			String filterText = filterTextField.getText();
			applicationInput.textFilterEnabled(this, filterText);
			filterEnabled = true;
			checkWidgetsState();
		}
	}

	@Override
	public String getText() {
		return ta.getText();
	}

	@Override
	public int getMaxTextFieldSize() {
		return Setup.getMaxTextFieldSize();
	}

	@Override
	public void appendText(String newText) {
		ta.append(newText);

	}

	@Override
	public void setViewAtPosition(int i) {
		ta.setCaretPosition(i);
	}

	@Override
	public void setText(String newText) {
		ta.setText(newText);
	}

	@Override
	public void keyTyped(KeyEvent e) {
	}

	private void checkWidgetsState() {
		if(filterEnabled != disablefilterBtn.isEnabled()) {
			disablefilterBtn.setEnabled(filterEnabled);
		}
		String filterText = filterTextField.getText();
		if (filterText != null && filterText.length() > 0) {
			filterBtn.setEnabled(true);
		} else {
			filterBtn.setEnabled(false);
		}
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		checkWidgetsState();
	}

	public JButton getDefaultButton() {
		return filterBtn;
	}

	@Override
	public int getNumberOfVisibleRows() {
		int lines = ta.getSize().height / lineHeight;
		return lines;
	}

	@Override
	public LogID getLogID() {
		return logID;
	}
}
